/*
 * @Author: lineCode 1145938682@qq.com
 * @Date: 2024-12-22 14:10:21
 * @LastEditors: lineCode 1145938682@qq.com
 * @LastEditTime: 2024-12-29 06:16:12
 * @FilePath: /backend/shorthand-modules/shorthand-modules-content/src/main/java/com/angus/modules/content/service/IContentService.java
 */
package com.angus.modules.content.service;

import cn.hutool.core.collection.CollUtil;
import com.angus.common.core.exception.ServiceException;
import com.angus.common.core.utils.poi.ExcelUtil;
import com.angus.common.core.web.domain.AjaxResult;
import com.angus.modules.content.vo.ContentResultVo;
import com.angus.modules.content.vo.ContentResultVoV2;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public interface IContentService<T> {

    Integer GROUP_COUNT = 50;
    String importContent(MultipartFile file, String courseCode);

    List<T> getContentList(String courseCode,Integer unitNum);

    AjaxResult addContent(T content);

    Boolean updateContent(T content);

    Integer deleteByIds(List<Long> ids, Integer unitNum,String courseCode);

    ContentResultVo getContentByGroupId(String courseCode, Integer unitNum, Long groupId);

    ContentResultVoV2 getContentByGroupFlag(String courseCode, Integer unitNum, String groupFlag);

    AjaxResult syncSortID();


    @SuppressWarnings("hiding")
    default <T> List<T> readExcelFile(MultipartFile file, Class<T> clazz) throws Exception {
        ExcelUtil<T> util = new ExcelUtil<>(clazz);
        List<T> contentList = util.importExcel(file.getInputStream());
        if (CollUtil.isEmpty(contentList)) {
            throw new ServiceException("导入内容数据不能为空！");
        }
        return contentList;
    }


    @SuppressWarnings("hiding")
    default <T> List<List<Integer>> checkIfUnitIsComplete(Map<Integer, List<T>> contentMap) {
        List<Integer> keys = new ArrayList<>(contentMap.keySet());
        int batchSize = GROUP_COUNT;
        return partition(keys, batchSize);
    }

    default List<List<Integer>> partition(List<Integer> keys, int batchSize) {
        List<List<Integer>> batches = new ArrayList<>();
        for (int i = 0; i < keys.size(); i += batchSize) {
            int end = Math.min(i + batchSize, keys.size());
            List<Integer> batch = keys.subList(i, end);
            batches.add(batch);
        }
        return batches;
    }

}
